/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zyao89.view.zloading.slice;

import com.zyao89.view.zloading.ResourceTable;
import com.zyao89.view.zloading.ZLoadingView;
import com.zyao89.view.zloading.Z_TYPE;
import com.zyao89.view.zloading.util.Log;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;

/**
 * 功能描述
 *
 * @author jjy
 * @since 2021-03-22
 */
public class ShowAbilitySlice extends AbilitySlice {
    private static final String LOADING_TYPE = "loading_type";
    private static final String TAG = "Zloading ShowAbilitySlice";
    private static final int EVENT_MESSAGE_NORMAL = 1;
    private int type;
    private EventRunner runner;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_show);
        type = intent.getIntParam(LOADING_TYPE, 0);
        Log.sysI(TAG, "type : " + type);
        initView(type);
    }

    /**
     * 功能描述
     *
     * @author jjy
     * @since 2021-03-22
     */
    public class MyEventHandler extends EventHandler {
        /**
         * 方法描述
         *
         * @param runner
         */
        public MyEventHandler(EventRunner runner) {
            super(runner);
        }

        @Override
        public void processEvent(InnerEvent event) {
            super.processEvent(event);
            if (event == null) {
                return;
            }
            int eventId = event.eventId;
            switch (eventId) {
                case EVENT_MESSAGE_NORMAL:
                    // 待执行的操作，由开发者定义
                    initView(type);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    private void initData() {
        // create()的参数是true时，则为托管模式
        runner = EventRunner.create(true);
        MyEventHandler myHandler = new MyEventHandler(runner);
        long param = 0L;
        InnerEvent normalInnerEvent = InnerEvent.get(EVENT_MESSAGE_NORMAL, param);
        myHandler.sendEvent(normalInnerEvent, 0, EventHandler.Priority.IMMEDIATE);
    }

    private void initView(int mlodingType) {
        ZLoadingView mzLoadingViewTopLeft = (ZLoadingView) findComponentById(ResourceTable.Id_loadingView_1);
        ZLoadingView mzLoadingViewTopRight = (ZLoadingView) findComponentById(ResourceTable.Id_loadingView_2);
        ZLoadingView mzLoadingViewCenter = (ZLoadingView) findComponentById(ResourceTable.Id_loadingView_3);
        mzLoadingViewTopLeft.setBindStateChangedListener(mzLoadingViewTopLeft);
        mzLoadingViewTopRight.setBindStateChangedListener(mzLoadingViewTopRight);
        mzLoadingViewCenter.setBindStateChangedListener(mzLoadingViewCenter);
        initBuilder(mzLoadingViewTopLeft, mlodingType);
        initBuilder(mzLoadingViewTopRight, mlodingType);
        initBuilder(mzLoadingViewCenter, mlodingType);
        ZLoadingView mzLoadingViewBotLeft = (ZLoadingView) findComponentById(ResourceTable.Id_loadingView_4);
        ZLoadingView mzLoadingViewBotRight = (ZLoadingView) findComponentById(ResourceTable.Id_loadingView_5);
        mzLoadingViewBotLeft.setBindStateChangedListener(mzLoadingViewBotLeft);
        mzLoadingViewBotRight.setBindStateChangedListener(mzLoadingViewBotRight);
        initBuilder(mzLoadingViewBotLeft, mlodingType);
        initBuilder(mzLoadingViewBotRight, mlodingType);
    }

    private void initBuilder(ZLoadingView mzLoadingView, int typeParam) {
        mzLoadingView.setLoadingBuilder(Z_TYPE.values()[typeParam]);
    }
}
